package cn.anjubao.cp.api.service;

import cn.anjubao.cp.api.common.Constants;
import cn.anjubao.cp.api.dto.*;
import cn.anjubao.cp.api.entity.ParkingRecord;
import cn.anjubao.cp.api.feign.ParkingLotFeignClient;
import cn.anjubao.cp.api.mapper.ParkingLotSlotMapper;
import cn.anjubao.cp.api.mapper.ParkingRecordMapper;
import cn.anjubao.cp.common.base.exception.BusinessException;
import cn.anjubao.cp.common.utils.DateHelper;
import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingLotBillRuleDto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingTotalFeeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Date;

@Service
public class ParkingLotSlotService {

    @Autowired
    private ParkingLotSlotMapper parkingLotSlotMapper;
    @Autowired
    private ParkingRecordMapper parkingRecordMapper;
    @Autowired
    private ParkingLotFeignClient parkingLotFeignClient;

    /**
     * 获取车位状态
     *
     * @param slotNum
     * @return
     */
    public SlotNumStatusResDto checkSlotNum(String slotNum) throws BusinessException {
        SlotNumStatusResDto slotNumStatusResDto = new SlotNumStatusResDto();
        SlotTypeDto resDto = parkingLotSlotMapper.getParkingLotTypeBySlotNum(slotNum);
        if(resDto == null){             // 车位不存在
            slotNumStatusResDto.setExist(false);
            return slotNumStatusResDto;
        }else {
            slotNumStatusResDto.setExist(true);
        }
        int slotState =1;
        if(StringUtils.isEmpty(resDto.getParkingRecordId())) slotState = 0;
        slotNumStatusResDto.setSlotState(slotState);
        //1:中杆
        if(resDto.getParkingType() == 2){
            slotNumStatusResDto.setParkingType(1);
            if (slotState==1){
                ApiResult<ParkingLotBillRuleDto> apiResult= parkingLotFeignClient.getBillRule(slotNum);
                if (apiResult.getCode()!=2000){
                    throw new BusinessException("-1",apiResult.getDesc());
                }
                int dayTimeSection = apiResult.getResult().getBillRuleSub().getDayTimeSection();
                int dayFeeSection = apiResult.getResult().getBillRuleSub().getDayFeeSection();
                slotNumStatusResDto.setMiddlePoleData(new MiddlePoleData().setDayFeeSection(dayFeeSection)
                        .setDayTimeSection(dayTimeSection)
                        .setMonitorTimeSection(Constants.monitor_time_section)
                        .setMonitorFeeSection(Constants.monitor_Fee_section));
            }
        }
        //2：低杆
        if(resDto.getParkingType() == 1){
            slotNumStatusResDto.setParkingType(2);
            if (slotState==1){
                ParkingRecord parkingRecord = parkingRecordMapper.selectById(resDto.getParkingRecordId());
                long parkingTime = (new Date().getTime() - parkingRecord.getInTime().getTime()) / 1000;
                ParkingTotalFeeDto feeDto= parkingLotFeignClient.
                        getParkingTotalFeeWithString(parkingRecord.getParkingLotCode(),
                                DateHelper.getDateString(parkingRecord.getInTime()),DateHelper.getDateString(new Date()))
                        .getResult();
                LowPoleOrderData lowPoleOrderData = new LowPoleOrderData();
                lowPoleOrderData.setParkingRecordId(resDto.getParkingRecordId());
                lowPoleOrderData.setOrderState(parkingRecord.getFeeState());
                lowPoleOrderData.setVehicle(parkingRecord.getVehicleNumber());
                lowPoleOrderData.setParkingLotName(parkingRecord.getParkingLotName());
                lowPoleOrderData.setSlotNum(slotNum);
                lowPoleOrderData.setParkingTime((int)parkingTime);
                lowPoleOrderData.setParkingFee(feeDto.getTotalFee());
                slotNumStatusResDto.setLowPoleOrderData(lowPoleOrderData);
            }
        }
        return slotNumStatusResDto;
    }
}
